package com.situ.water_BE.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.water_BE.pojo.BeOrder;
import com.situ.water_BE.pojo.BeOrderuser;
import com.situ.water_BE.service.BeFormulaService;
import com.situ.water_BE.service.BeHistoryService;
import com.situ.water_BE.service.BeOrderService;
import com.situ.water_BE.service.BeOrderuserService;
import com.situ.water_system.pojo.SyEmp;
import com.situ.water_system.pojo.SyMetertype;
import com.situ.water_user.pojo.UsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Angel
 * @since 2020-12-01
 */
@RestController
@RequestMapping("/be-order")
public class BeOrderController { //业扩工单
    @Autowired
    private BeOrderService beOrderService;
    @Autowired
    private BeOrderuserService beOrderuserService;
    @Autowired
    private BeHistoryService beHistoryService;
    @Autowired
    private BeFormulaService beFormulaService;


    /**
     * 添加用户申请
     *
     * @param beOrderuser
     * @param beOrder
     * @param session
     * @return
     */
    @PostMapping("/add")
    public String add(BeOrder beOrder,BeOrderuser beOrderuser, String type,String panDuAn, HttpSession session) { //添加Be_Order表 新用户
        SyEmp user = (SyEmp) session.getAttribute("user");
        SyEmp emp = new SyEmp();
        if (user == null) {
            emp.setId(10);
            session.setAttribute("user", emp);
        }
        user = (SyEmp) session.getAttribute("user");
        if("".equals(panDuAn) || null==panDuAn){
            String s = beOrderService.orderNo(type);
            if ("工单号已满".equals(s)) {
                return "工单号已满";
            }
            List<BeOrderuser> list = beOrderuser.getList();
            System.out.println(beOrder);
            if ("3".equals(type) || "4".equals(type) || "5".equals(type) || "6".equals(type) || "7".equals(type)) {
                beOrderuserService.addOrderUser(s, beOrderService.queryUserUserNo(beOrder.getOldUserNo()).
                        getDocNum(), beOrder);
            }
            if (list != null) {
                for (BeOrderuser hh : list) {
                    if (!"".equals(hh.getUserName()) && !"".equals(hh.getPhone())
                            && !"".equals(hh.getSMSPhone()) && !"".equals(hh.getAddress())) {
                        hh.setMeterTypeID(beOrder.getMeterTypeID());
                        beOrderuserService.addBE_OrderUser(hh, s);
                    }
                }
            }
            System.out.println(type);
            int i = beOrderService.add(beOrder, s, Integer.parseInt(type), user.getId());
            beHistoryService.add(s, user.getId());
            if (i >= 1) {
                return "保存成功";
            }
            return "保存失败";
        }else {
            List<BeOrderuser> list = beOrderuser.getList();
            System.out.println(beOrder.getOrderNo());
            int i = beOrderuserService.addBE_OrderUser2(list, beOrder.getOrderNo());
            if(i>=1){
                beOrderService.add2(beOrder,user.getId());
                beHistoryService.add(beOrder.getOrderNo(), user.getId());
                return "保存成功";
            }
            return "保存失败";
        }
    }

    @GetMapping("/queryAll")
    public List<BeOrder> queryAll() {
        return beOrderService.queryAll("初步审核");
    }
    /**
     * 处理初步审核
     *
     * @param id
     * @param text
     * @param pd
     * @param session
     * @return
     */
    @PutMapping("/updateStepID")
    public String updateStepID(String id, String text, Integer pd, HttpSession session, String lx) {//处理初步审核
        SyEmp user = (SyEmp) session.getAttribute("user");
        SyEmp emp = new SyEmp();
        if (user == null) {
            emp.setId(15);
            session.setAttribute("user", emp);
        }
        user = (SyEmp) session.getAttribute("user");
        if (pd == 1) {
            int i = beOrderService.updateStepID(id, user.getId(), text, 1, user.getId(), new Date());
            if (i == 1) {
                int i1 = beHistoryService.updateStepIdAdd(id, beOrderService.beFlowQueryId(lx, 2, 25)
                        , user.getId(), pd);
                if (i1 == 1) {
                    return "撤回成功";
                }
            }
        } else {
            int i = beOrderService.updateStepID(id, user.getId(), text,
                    beOrderService.beFlowQueryId(lx, 2, 15), user.getId(), new Date());
            if (i >= 1) {
                int i1 = beHistoryService.updateStepIdAdd(id, beOrderService.beFlowQueryId(lx, 2, 5),
                        user.getId(), 0);
                if (i1 >= 1) {
                    return "发送成功";
                }
            }
        }
        return "发送失败";
    }

    @GetMapping("/gjQuery")
    public List<Map> gjQuery(BeOrder beOrder) { //查询工单管理页面
        List<Map> map = beOrderService.gjQuery(beOrder);
        return map;
    }
    @GetMapping("/gjQueryCount")
    public int gjQueryCount(BeOrder beOrder) { //查询工单管理页面
        return beOrderService.gjQueryCount(beOrder);
    }

    @GetMapping("/bePayQueryAll")
    public List<BeOrder> payQueryAll() { //查询交施工费页面
        return beOrderService.queryAll("交施工费");
    }

    @GetMapping("/beAbortQueryId")
    public Map queryId(String id) { //查询be_abort.html并终止工单
        Map map = null;
        System.out.println(id);
        if (id != null && !"".equals(id)) {
            map = beOrderService.queryId(id);
        }
        return map;
    }
    @PutMapping("/be_abortUpdate")
    public String be_abortUpdate(BeOrder beOrder,HttpSession session) { //查询be_abort.html并终止工单
        SyEmp user = (SyEmp) session.getAttribute("user");
        SyEmp emp = new SyEmp();
        if (user == null) {
            emp.setId(15);
            session.setAttribute("user", emp);
        }
        user = (SyEmp) session.getAttribute("user");
        int i = beOrderService.updateStepID2(beOrder,user.getId());
        if(i>=1){
            return "成功";
        }else {
            return "失败";
        }
    }

    /**
     * 处理交施工费
     *
     * @param beOrder
     * @param beOrderuser
     * @param pd
     * @param session
     * @return
     */
    @PutMapping("/be__payFormUpdate")
    public String be__payFormUpdate(BeOrder beOrder, BeOrderuser beOrderuser, Integer pd, String lx, HttpSession session) {
        SyEmp user = (SyEmp) session.getAttribute("user");
        SyEmp emp = new SyEmp();
        if (user == null) {
            emp.setId(20);
            session.setAttribute("user", emp);
        }
        user = (SyEmp) session.getAttribute("user");
        if (pd == 1) {
            int i = beOrderService.updateStepID(beOrder.getOrderNo(),
                    beOrderService.beFlowQueryId(lx, 3, 25), user.getId(), new Date(), 1);
            if (i >= 1) {
                beHistoryService.updateStepIdAdd(beOrder.getOrderNo(),
                        beOrderService.beFlowQueryId(lx, 3, 25), user.getId(), 1);
                return "撤回成功";
            }
        } else if (pd == 2) {
            List<BeOrderuser> list = beOrderuser.getList();
            int i = beOrderuserService.updateBe__payForm(list, beOrder, user.getId(), 0,
                    beOrderService.beFlowQueryId(lx, 3, 15),beOrderService.beFlowQueryId(lx, 3, 5));
            if (i >= 1) {
                return "发送成功";
            }
        } else {
            List<BeOrderuser> list = beOrderuser.getList();
            beOrder.setPayDate(new Date());
            int i = beOrderuserService.updateBe__payForm2(list, beOrder, user.getId(), lx, 3, false);
            if (i >= 1) {
                return "保存成功";
            }
        }
        return "失败";
    }

    @GetMapping("/be_contractQueryAll")
    public List<BeOrder> be_contractQueryAll() {
        return beOrderService.queryAll("供水协议");
    }

    /**
     * 处理水费清算
     *
     * @param beOrder
     * @param lx
     * @param pd
     * @param session
     * @return
     */
    @PutMapping("/be_billclearFormUpdate")
    public String be_billclearFormUpdate(BeOrder beOrder, String lx, Integer pd, HttpSession session) {
        SyEmp user = (SyEmp) session.getAttribute("user");
        SyEmp emp = new SyEmp();
        if (user == null) {
            emp.setId(1);
            session.setAttribute("user", emp);
        }
        user = (SyEmp) session.getAttribute("user");
        if (pd == 1) {
            int i = beOrderService.updateStepID(beOrder.getOrderNo(),
                    beOrderService.beFlowQueryId(lx, 4, 25), user.getId(), new Date(), 10);
            if (i >= 1) {
                int add = beHistoryService.updateStepIdAdd(beOrder.getOrderNo(),
                        beOrderService.beFlowQueryId(lx, 4, 25), user.getId(), 1);
                if (add >= 1) {
                    return "撤回成功";
                }
            }
        } else {
            int i = beOrderService.updateStepID(beOrder.getOrderNo(),
                    beOrderService.beFlowQueryId(lx, 4, 15), user.getId(), new Date(), 10);
            if (i >= 1) {
                int add1 = beHistoryService.updateStepIdAdd(beOrder.getOrderNo(),
                        beOrderService.beFlowQueryId(lx, 4, 5), user.getId(), 0);
                return "发送成功";
            }
        }
        return "失败";
    }

    /**
     * 处理供水协议
     *
     * @param beOrder
     * @param beOrderuser
     * @param pd          判断
     * @param session     sessionId
     * @return
     */
    @PutMapping("/be_contractFormUpdate")
    public String be_contractFormUpdate(BeOrder beOrder, BeOrderuser beOrderuser, Integer pd,
                                        String lx, HttpSession session) {
        SyEmp user = (SyEmp) session.getAttribute("user");
        SyEmp emp = new SyEmp();
        if (user == null) {
            emp.setId(1);
            session.setAttribute("user", emp);
        }
        user = (SyEmp) session.getAttribute("user");

        if (pd == 1) {
            int i = beOrderService.updateStepID(beOrder.getOrderNo(),
                    beOrderService.beFlowQueryId(lx, 5, 25), user.getId(), new Date(), 1);
            if (i >= 1) {
                int add = beHistoryService.updateStepIdAdd(beOrder.getOrderNo(),
                        beOrderService.beFlowQueryId(lx, 5, 25), user.getId(), 1);
                if (add >= 1) {
                    return "撤回成功";
                }
            }
        } else if (pd == 2) {
            List<BeOrderuser> list = beOrderuser.getList();
            int i = beOrderService.updateStepID(beOrder.getOrderNo(),
                    beOrderService.beFlowQueryId(lx, 5, 15), user.getId(), new Date(), -1);
            if (i >= 1) {
                int add1 = beHistoryService.updateStepIdAdd(beOrder.getOrderNo(),
                        beOrderService.beFlowQueryId(lx, 5, 5), user.getId(), 0);
                if (add1 >= 1) {
                    if (list != null) {
                        beFormulaService.addBeFormula(list, beOrder);
                    }
                    return "发送成功";
                }
            }
        } else {
            List<BeOrderuser> list = beOrderuser.getList();
            int i = 0;
            if (!"2".equals(lx)) {
                i = beOrderuserService.updateBe__payForm2(list, beOrder, user.getId(), lx, 4, true);
            } else {
                i = beOrderuserService.updateBe__payForm2(list, beOrder, user.getId(), lx, 5, true);
            }
            if (i >= 1) {
                return "保存成功";
            }
        }
        return "失败";
    }

    @GetMapping("/be_workingQueryAll")
    public List<BeOrder> be_workingQueryAll() {
        return beOrderService.queryAll("施工竣工");
    }

    /**
     * 处理施工竣工
     *
     * @param beOrder
     * @param beOrderuser
     * @param pd
     * @param session
     * @param lx
     * @return
     */
    @PutMapping("be_workingFormUpdate")
    public String be_workingFormUpdate(BeOrder beOrder, BeOrderuser beOrderuser, Integer pd, HttpSession session, String lx) {
        SyEmp user = (SyEmp) session.getAttribute("user");
        SyEmp emp = new SyEmp();
        if (user == null) {
            emp.setId(2);
            session.setAttribute("user", emp);
        }
        user = (SyEmp) session.getAttribute("user");
        List<BeOrderuser> list = beOrderuser.getList();

        if (pd == 1) {
            int i = beOrderService.updateStepID(beOrder.getOrderNo(),
                    beOrderService.beFlowQueryId(lx, 6, 25), user.getId(), new Date(), 1);//>0就调用没有修改交施工费的日期
            if (i >= 1) {
                int add = beHistoryService.updateStepIdAdd(beOrder.getOrderNo(),
                        beOrderService.beFlowQueryId(lx, 6, 25), user.getId(), 1);//1表示是否撤回
                if (add >= 1) {
                    return "撤回成功";
                }
            }
        } else if (pd == 2) {
            System.out.println(beOrder.getAreaID());
            int i = beOrderuserService.updateBe__payForm2(list, beOrder, user.getId(),
                    lx, beOrderService.beFlowQueryId(lx, 6, 15), false);
            if (i >= 1) {
                int add1 = beHistoryService.updateStepIdAdd(beOrder.getOrderNo(),
                        beOrderService.beFlowQueryId(lx, 6, 5), user.getId(), 0);
                if (add1 >= 1) {
                    return "发送成功";
                }
            }
        } else {
            int i = beOrderuserService.updateBe__payForm2(list, beOrder, user.getId(), lx,
                    beOrderService.beFlowQueryId(lx, 6, 5), false);
            if (i >= 1) {
                return "保存成功";
            }
        }
        return "失败";
    }

    /**
     * 查询水表类型
     *
     * @return
     */
    @GetMapping("/querySyMeterType")
    public List<SyMetertype> querySyMeterType() {
        return beOrderService.QuerySyMeterType();
    }

    /**
     * 根据水表id查询
     *
     * @param sid
     * @return
     */
    @GetMapping("/queryIdSyMeterType")
    public SyMetertype queryIdSyMeterType(Integer sid) {
        return beOrderService.QueryIdSyMeterType(new QueryWrapper<SyMetertype>().eq("id", sid));
    }

    @GetMapping("/queryIdBeOrder")
    public BeOrder queryIdBeOrder(String orderNo) {
        System.out.println("111");
        return beOrderService.queryIdBeOrder(new QueryWrapper<BeOrder>().eq("orderNo", orderNo));
    }

    @GetMapping("/be_openQueryAll")
    public List<BeOrder> be_openQueryAll() {
        return beOrderService.queryAll("通水停水");
    }

    /**
     * 处理通水停水
     *
     * @param orderNo
     * @param lx
     * @param pd
     * @param session
     * @return
     */
    @PutMapping("/be_openFormUpdate")
    public String be_openFormUpdate(String orderNo, String lx, Integer pd, HttpSession session) {
        SyEmp user = (SyEmp) session.getAttribute("user");
        SyEmp emp = new SyEmp();
        if (user == null) {
            emp.setId(3);
            session.setAttribute("user", emp);
        }
        user = (SyEmp) session.getAttribute("user");
        if (pd == 1) {
            int i = beOrderService.updateStepID(orderNo,
                    beOrderService.beFlowQueryId(lx, 7, 25), user.getId(), new Date(), 10);//>0就调用没有修改交施工费的日期
            if (i >= 1) {
                int add = beHistoryService.updateStepIdAdd(orderNo,
                        beOrderService.beFlowQueryId(lx, 7, 25), user.getId(), 1);//1表示是否撤回中是
                if (add >= 1) {
                    return "撤回成功";
                }
            }
        } else {
            int i = beOrderService.updateStepID(orderNo,
                    beOrderService.beFlowQueryId(lx, 7, 15), user.getId(), new Date(), 1);
            if (i >= 1) {
                int add1 = beHistoryService.updateStepIdAdd(orderNo,
                        beOrderService.beFlowQueryId(lx, 7, 5), user.getId(), 0);
                if (add1 >= 1) {
                    return "发送成功";
                }
            }
        }
        return "失败";
    }

    /**
     * 查询档案存档
     *
     * @return
     */
    @GetMapping("/be_saveQueryAll")
    public List<BeOrder> be_saveQueryAll() {
        return beOrderService.queryAll("档案存档");
    }

    /**
     * 处理档案存档
     *
     * @param beOrder
     * @param beOrderuser
     * @param lx          判断用户申请的类型
     * @param pd          判断点击那个按钮实现不同策略
     * @param session
     * @return
     */
    @PutMapping("/be_saveFormUpdate")
    public String be_saveFormUpdate(BeOrder beOrder, BeOrderuser beOrderuser, String lx, Integer pd, HttpSession session) {
        SyEmp user = (SyEmp) session.getAttribute("user");
        SyEmp emp = new SyEmp();
        if (user == null) {
            emp.setId(4);
            session.setAttribute("user", emp);
        }
        user = (SyEmp) session.getAttribute("user");
        List<BeOrderuser> list = beOrderuser.getList();
        if (pd == 1) {
            int i = beOrderService.updateStepID(beOrder.getOrderNo(),
                    beOrderService.beFlowQueryId(lx, 8, 25), user.getId(), new Date(), 10);//>0就调用没有修改交施工费的日期
            if (i >= 1) {
                int add = beHistoryService.updateStepIdAdd(beOrder.getOrderNo(),
                        beOrderService.beFlowQueryId(lx, 8, 25), user.getId(), 1);//1表示是否撤回中是
                if (add >= 1) {
                    return "撤回成功";
                }
            }
        } else if (pd == 2) {
            int i = beOrderuserService.updateBe__payForm3(list, beOrder, user.getId(), 0, true, lx);
            if (i >= 1) {
                int add1 = beHistoryService.updateStepIdAdd(beOrder.getOrderNo(),
                        beOrderService.beFlowQueryId(lx, 8, 5), user.getId(), 0);//0表示没有被撤回
                beHistoryService.updateStepIdAdd(beOrder.getOrderNo(),
                        0, user.getId(), 0);//pd:0表示没有被撤回
                if (add1 >= 1) {
                    return "发送成功";
                }
            }
        } else {
            int i = beOrderuserService.updateBe__payForm3(list, beOrder, user.getId(),
                    beOrderService.beFlowQueryId(lx, 8, 5), false, lx);
            if (i >= 1) {
                return "保存成功";
            }
        }
        return "失败";
    }

    /**
     * 根据用户编码查询用户
     *
     * @param uid
     * @return
     */
    @GetMapping("/be_request2UserQueryId")
    public UsUser be_request2UserQueryId(String uid) {
        return beOrderService.userQueryId(new QueryWrapper<UsUser>().
                eq("userNo", uid).ne("Disabled",true));
    }

    /**
     * 查询水费清算页面
     *
     * @return
     */
    @GetMapping("/be_BillclearQueryAll")
    public List<BeOrder> be_billclearQueryAll() {
        return beOrderService.queryAll("水费清算");
    }

    /**
     * 水费清算查询原用户
     *
     * @param orderNo
     * @return
     */
    @GetMapping("/queryIdUserAndOrder")
    public Map queryIdUserAndOrder(String orderNo) {
        return beOrderService.queryIdUserAndOrder(orderNo);
    }


    /**
     * 查询用户申请
     *
     * @return
     */
    @GetMapping("/be_requestOrderQueryAll")
    public List<BeOrder> be_requestOrderQueryAll() {
        return beOrderService.queryAll("用户申请");
    }

    @GetMapping("/queryAllOrderAndOrderUser")
    public List<Map> queryAllOrderAndOrderUser(String orderNo){
        return beOrderuserService.queryAllOrderAndOrderUser(orderNo);
    }

    @GetMapping("/queryAllBeOrderUserSQL")
    public  List<Map> queryAllBeOrderUserSQL(Integer[] arr,String payDate1,String payDate2,Integer curr,Integer limit){
        Map<String,Object> map=new HashMap<String,Object>();
        if(arr!=null && arr.length>0){
            map.put("arr",arr);
        }
        map.put("curr",curr);
        map.put("limit",limit);
        System.out.println(curr);
        System.out.println(limit);
        map.put("payDate1",payDate1);
        map.put("payDate2",payDate2);
        List<Map> maps = beOrderService.queryAllBeOrderUserSQL(map);
        return maps;
    }
    @GetMapping("/queryAllBeOrderUserSQLConut")
    public  Integer queryAllBeOrderUserSQLConut(Integer[] arr,String payDate1,String payDate2,Integer curr,Integer limit){
        Map<String,Object> map=new HashMap<String,Object>();
        if(arr!=null && arr.length>0){
            map.put("arr",arr);
        }
        map.put("curr",curr);
        map.put("limit",limit);
        System.out.println(curr);
        System.out.println(limit);
        map.put("payDate1",payDate1);
        map.put("payDate2",payDate2);
        int i = beOrderService.queryAllBeOrderUserSQLCount(map);
        return i;
    }
    @GetMapping("/queryAllSY_MeterType")
    public List<SyMetertype> queryAllSY_MeterType(){
        return beOrderService.queryAllSY_MeterType();
    }

    @GetMapping("/limitBeOrderSQL")
    public List<Map> limitBeOrderSQL(){
        return beOrderService.limitBeOrderSQL();
    }
}

